package cn.jdy.plugins.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jdy.authority.entity.UserRole;
import cn.jdy.authority.service.UserRoleService;
import cn.jdy.authority.service.UserService;
import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.Chat;
import cn.jdy.plugins.entity.Live;

/**
 * @desc   交流区内容操作
 * @author ningqian
 * @date   2017-3-23
 */
@Service
public class ChatService extends BaseDao<Chat>{
	
	@Autowired
	private UserRoleService userRoleService;
	@Autowired
	private UserService userService;
	@Autowired
	private LiveService liveService;
	
	/**
	 * 按条件查询
	 * @param Kindergarten
	 * @return
	 */
	public List<Chat> query(Chat chat, String... date) {
		if(null == chat) return super.findAll();
		if(chat.getiDisplayLength() > 0){
		  return super.find(createFilter(chat, date),chat.getiDisplayStart(),chat.getiDisplayLength());
		}
		return super.find(createFilter(chat, date));
	}

	public Long count(Chat chat, String... date) {
		return super.count(createFilter(chat, date));
	}
	
	/**
	 * 拼装条件(直播id、创建时间)
	 * @param Kindergarten
	 * @param date
	 * @return
	 */
	private DetachedCriteria createFilter(Chat chat, String... date) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	  
	  if(null != chat.getLiveId()){
		  dc.add(Restrictions.eq("liveId", chat.getLiveId()));
	  }
	  
	  if(StringUtil.isNotEmpty(chat.getChatType())){
		  dc.add(Restrictions.eq("chatType", chat.getChatType()));
	  }
	  if(StringUtil.isNotEmpty(chat.getChatContent())){
		  dc.add(Restrictions.like("chatContent", chat.getChatContent(),MatchMode.ANYWHERE));
	  }
	  
	  if(date.length>0 && !StringUtil.isNullOrEmpty(date[0]) && !StringUtil.isNullOrEmpty(date[1])){
	    dc.add(Restrictions.between("createTime", date[0],date[1]));
	  }
	  
	  dc.addOrder(Order.desc("createTime"));
		return dc;
	}
	
	
	
	
	/**
	 * 按条件查询 分页
	 * @param Kindergarten
	 * @return
	 */
	public List<Chat> queryHistory(Long liveId, String createTime,int pageNo,int pageSize) {
		return super.find(createFilter(liveId, createTime),pageNo,pageSize);
	}
	
//	public List<Chat> queryHistory(Long liveId, String createTime) {
//		return super.find(createFilter(liveId, createTime));
//	}
	
	public Long count(Long liveId, String createTime) {
		return super.count(createFilter(liveId, createTime));
	}
	
	private DetachedCriteria createFilter(Long liveId, String createTime) {
		  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		  
		  if(null != liveId){
			  dc.add(Restrictions.eq("liveId", liveId));
		  }
		  
		  if(StringUtil.isNotEmpty(createTime)){
			  dc.add(Restrictions.lt("createTime",createTime));
		  }
		  
		  
		 dc.addOrder(Order.desc("createTime"));
		  return dc;
		}
		
	public List<Chat> queryids(Object[] data,Chat chat,String... date) {
		if(null == chat) return super.findAll();
		if(chat.getiDisplayLength() > 0){
		  return super.find(createFilterids(data,chat, date),chat.getiDisplayStart(),chat.getiDisplayLength());
		}
		return super.find(createFilterids(data,chat, date));
	}

	public Long countids(Object[] chat,Chat ct, String... date) {
		return super.count(createFilterids(chat,ct, date));
	}
	//跟多个直播id去查询
	private DetachedCriteria createFilterids(Object[]  data,Chat chat, String... date)  {
		  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
			  dc.add(Restrictions.in("liveId", data));
			  if(StringUtil.isNotEmpty(chat.getChatType())){
				  dc.add(Restrictions.eq("chatType", chat.getChatType()));
			  }
			  if(StringUtil.isNotEmpty(chat.getChatContent())){
				  dc.add(Restrictions.like("chatContent", chat.getChatContent(),MatchMode.ANYWHERE));
			  }
		 dc.addOrder(Order.desc("createTime"));
		  return dc;
		}

	/*获取权限的live*/
	public  List<Live> organlist(String  userid,String organid) {
		List<UserRole> list = userRoleService.find("userid", userid);
		int roleid = 0;
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getRoleid()
					.equals("4028b3044de1a396014de1ba2b2e0000")
					|| list.get(i).getRoleid()
							.equals("ff8080814db89f0a014db8d169290000")) {
				roleid = 1;
			}
		}
		if(roleid==0){
		//	String Organid=userService.findOne("userid",userid).getOrganid();
			return liveService.find("appid",organid);
		}else{
			return liveService.findAll();
		}


	}

		
}
